package com.gimi.cloud.jimi.merchantZone.service.product.impl;

import java.math.BigDecimal;
import java.util.*;

import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.*;
import com.gimi.cloud.jimi.common.utils.oss.AliOSSUtil;
import com.gimi.cloud.jimi.dao.dao.product.*;
import com.gimi.cloud.jimi.dao.dao.stroe.StoreDao;
import com.gimi.cloud.jimi.dto.product.*;
import com.gimi.cloud.jimi.dto.store.StoreDTO;
import com.gimi.cloud.jimi.entity.product.*;
import com.gimi.cloud.jimi.merchantZone.service.product.SkuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.dao.dao.prod.ProdBrandDao;
import com.gimi.cloud.jimi.dao.dao.prod.ProdCatModelDao;
import com.gimi.cloud.jimi.dao.dao.prod.ProdCategoryDao;
import com.gimi.cloud.jimi.dto.prod.ProdBrandDTO;
import com.gimi.cloud.jimi.dto.prod.ProdCatModelDTO;
import com.gimi.cloud.jimi.dto.prod.ProdCategoryDTO;
import com.gimi.cloud.jimi.merchantZone.service.product.ProductService;

@Service
public class ProductServiceImpl implements ProductService {
    private static Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class);
    private final ProductDao productDao;
    private final ProductStatusDao productStatusDao;
    private final ProductImgDao productImgDao;
    private final ProductSkuDao productSkuDao;
    private final ProductSkuImgDao productSkuImgDao;
    private final ProductValueAddDao productValueAddDao;
    private final ProdCategoryDao prodCategoryDao;
    private SkuService skuService;
    private final StoreDao storeDao;
    private final LogisticsCompanyDao logisticsCompanyDao;


    private final ProdBrandDao prodBrandDao;
    private final ProdCatModelDao prodCatModelDao;
    private final LogisticsDao logisticsDao;

    @Autowired
    public ProductServiceImpl(ProductDao productDao, ProductStatusDao productStatusDao, ProductImgDao productImgDao,
                              ProductSkuDao productSkuDao, ProductSkuImgDao productSkuImgDao,
                              ProductValueAddDao productValueAddDao, ProdCategoryDao prodCategoryDao,
                              ProdBrandDao prodBrandDao, ProdCatModelDao prodCatModelDao,LogisticsDao logisticsDao,
                              SkuService skuService, StoreDao storeDao,LogisticsCompanyDao logisticsCompanyDao) {
        this.productDao = productDao;
        this.productStatusDao = productStatusDao;
        this.productImgDao = productImgDao;
        this.productSkuDao = productSkuDao;
        this.productSkuImgDao = productSkuImgDao;
        this.productValueAddDao = productValueAddDao;
        this.prodCategoryDao = prodCategoryDao;
        this.prodBrandDao = prodBrandDao;
        this.prodCatModelDao = prodCatModelDao;
        this.skuService = skuService;
        this.storeDao = storeDao;
        this.logisticsCompanyDao = logisticsCompanyDao;
        this.logisticsDao = logisticsDao;

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ProductDto addProduct(ProductDto pdd) {
        String prodId = SeqFactory.getProdId();
        String prodCatId = pdd.getProdCatId();
        if (StringUtils.isBlank(prodId)) {
            pdd.setResponseCode(ResponseCodeEnum.PROD_ID_ERR);
            return pdd;
        }
        //校验品类是否是三级品类
        int catnum = productDao.getProdCat(prodCatId);
        if (catnum != 1) {
            pdd.setResponseCode(ResponseCodeEnum.PROD_PARAMS_ERR);
            return pdd;
        }
        pdd.setProdId(prodId);
        pdd.setMerchantId("1");
        pdd.setStoreId(pdd.getStoreId());
        //pdd.setProdPrice(new BigDecimal("10.23"));
        //pdd.setProdRentPrice();
        Map<String,BigDecimal> mprice = getMinAndMaxPrice(pdd);
        pdd.setProdMinPrice(mprice.get("min"));
        pdd.setProdMaxPrice(mprice.get("max"));
        pdd.setMerchantProdBarCode(prodId);
        pdd.setAdImg(AliOSSUtil.getFilePath(pdd.getAdImg()));
        //添加商品表数据
        productDao.insertSelectiveDto(pdd);
        //添加商品增值服务表数据
        List<ProductValueAddDTO> valueAddList = pdd.getProductValueAddEntity();
        List<ProductValueAddEntity> valueAddEntityListList = new ArrayList<>();
        if(valueAddList != null && valueAddList.size() > 0){
            for (ProductValueAddDTO valueAdd : valueAddList) {
                valueAdd.setProdId(prodId);
                ProductValueAddEntity temp = new ProductValueAddEntity();
                MyBeanUtils.copyProperties(valueAdd,temp);
                valueAddEntityListList.add(temp);
            }
            productValueAddDao.saveBatch(valueAddEntityListList);
        }
        //添加商品图片
        List<ProductImgDTO> prodImlList = pdd.getProductImgEntity();
        List<ProductImgEntity> prodImlEntityList = new ArrayList<>();
        for (ProductImgDTO imgs : prodImlList) {
            imgs.setProdId(prodId);
            imgs.setImgUrl(AliOSSUtil.getFilePath(imgs.getImgUrl()));

            ProductImgEntity temp = new ProductImgEntity();
            MyBeanUtils.copyProperties(imgs,temp);
            prodImlEntityList.add(temp);
        }
        productImgDao.saveBatch(prodImlEntityList);
        //添加商品状态
        ProductStatusEntity prodStatus = new ProductStatusEntity();
        Date nowTime = new Date();
        prodStatus.setProdId(prodId);
        prodStatus.setProdCreateOperator(pdd.getStoreId());
        prodStatus.setProdCreateTime(nowTime);
        prodStatus.setProdStatus(2);//默认待上架
        productStatusDao.insertSelective(prodStatus);
        //添加商品sku信息
        List<ProductSkuDto> productSku = pdd.getProductSkuDto();
        for (ProductSkuDto prodSku : productSku) {
            prodSku.setProdId(prodId);
            String prodSkuId = UUIDUtil.get32UUID();
            prodSku.setSkuId(prodSkuId);
            productSkuDao.insertSelectiveDto(prodSku);
            List<ProductSkuImgDTO> skuImg = prodSku.getProductSkuImgEntity();
            List<ProductSkuImgEntity> productSkuImgEntityList = new ArrayList<>();
            if (skuImg != null && skuImg.size() > 0) {
                for (ProductSkuImgDTO skuImgs : skuImg) {
                    skuImgs.setSkuId(prodSkuId);
                    skuImgs.setImgUrl(AliOSSUtil.getFilePath(skuImgs.getImgUrl()));

                    ProductSkuImgEntity temp = new ProductSkuImgEntity();
                    MyBeanUtils.copyProperties(skuImgs,temp);
                    productSkuImgEntityList.add(temp);
                }

                productSkuImgDao.saveBatch(productSkuImgEntityList);
            }
        }

        //添加物流信息
        addLogistics(pdd,prodId);
        return pdd;
    }

    /*private String getLogisticsValues(List<LogisticsDTO> logisticsList){
        StringBuilder sb = new StringBuilder();
        if(logisticsList == null || logisticsList.size() == 0){
            throw
        }
        return "";
    }*/

    @Override
    public ProductDto getProduct(String prodId) throws Exception {
        //TODO: 验证是否是当前店铺
        //只有待上架商品才能修改
        int num = productStatusDao.getProdStatus(prodId);
        if (num == 0) {
            throw new MsgException(ResponseCodeEnum.PROD_STATUS_ERR);
        }
        ProductDto dto = productDao.selectByProdId(prodId);
        if (dto != null) {
//            try {
//                dto.setSpecParameter(new String(dto.getSpecParameters(), "utf-8"));
//            } catch (UnsupportedEncodingException e) {
//                logger.info("商品规格参数转UTF-8失败");
//                return null;
//            }
            dto.setAdImg(AliOSSUtil.getFileUrl(dto.getAdImg(),AliOSSUtil.getNoEffictDateByDay(1)));
            try {
                String catId = dto.getProdCatId();
                dto.setFirstCatId(catId.substring(0, 2));
                dto.setSecondCatId(catId.substring(0, 5));
            } catch (Exception e) {
                logger.error(e.getMessage());
                throw new MsgException(ResponseCodeEnum.PROD_PARAMS_ERR);
            }

            dto.setProductValueAddEntity(productValueAddDao.selectByPrimaryKey(prodId));
            List<ProductImgDTO> productImgList = productImgDao.selectByprodId(prodId);
            for (ProductImgDTO temp : productImgList){
                temp.setImgUrl(AliOSSUtil.getFileUrl(temp.getImgUrl(),AliOSSUtil.getNoEffictDateByDay(1)));
            }
            dto.setProductImgEntity(productImgList);
            List<ProductSkuDto> skuDto = productSkuDao.selectByProdId(prodId);
            if (skuDto != null && skuDto.size() > 0) {
                for (ProductSkuDto skus : skuDto) {
                    List<ProductSkuImgDTO> list = productSkuImgDao.selectBySkuId(skus.getSkuId());
                    if (list != null && list.size() > 0) {
                        for(ProductSkuImgDTO temp : list){
                            temp.setImgUrl(AliOSSUtil.getFileUrl(temp.getImgUrl(),AliOSSUtil.getNoEffictDateByDay(1)));
                        }
                        skus.setProductSkuImgEntity(list);
                    } else {
                        skus.setProductSkuImgEntity(new ArrayList<>());
                    }
                    skus.setSkuValueDetail(skuService.getSkuModelDetail(skus.getSkuValue()));
                }
            }
            dto.setProductSkuDto(skuDto);
            //添加物流公司
            List<LogisticsDTO> checkedLogistics = logisticsDao.logisticsList(prodId);
            if(checkedLogistics == null){
                checkedLogistics = new ArrayList<>();
            }
            dto.setCheckedLogistics(checkedLogistics);
            dto.setCheckedLogisticsTemp(new ArrayList());
        }
        return dto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ProductDto updateProduct(ProductDto pdd) throws Exception {
        //只有待上架商品才能修改
        //TODO: 验证是否是当前店铺
        int num = productStatusDao.getProdStatus(pdd.getProdId());
        if (num > 0) {
            String handleImg = AliOSSUtil.getFilePath(pdd.getAdImg());
            pdd.setAdImg(handleImg);
            productDao.updateByPrimaryKeyDto(pdd);
            List<ProductValueAddDTO> valueAddList = pdd.getProductValueAddEntity();
            List<ProductValueAddEntity> valueAddEntityList = new ArrayList<>();
            if (valueAddList != null && valueAddList.size() > 0) {
                for(ProductValueAddDTO entity : valueAddList){
                    entity.setProdId(pdd.getProdId());//初始化prodId

                    ProductValueAddEntity temp = new ProductValueAddEntity();
                    MyBeanUtils.copyProperties(entity,temp);
                    valueAddEntityList.add(temp);
                }
                productValueAddDao.deleteByPrimaryKey(pdd.getProdId());
                productValueAddDao.saveBatch(valueAddEntityList);
            }
            List<ProductImgDTO> prodImgList = pdd.getProductImgEntity();
            List<ProductImgEntity> prodImgEntityList = new ArrayList<>();
            if (prodImgList != null && prodImgList.size() > 0) {
                productImgDao.deleteByProdId(pdd.getProdId());
                for(ProductImgDTO pri : prodImgList) {
                    pri.setImgUrl(AliOSSUtil.getFilePath(pri.getImgUrl()));

                    ProductImgEntity temp = new ProductImgEntity();
                    MyBeanUtils.copyProperties(pri,temp);
                    prodImgEntityList.add(temp);
                }
                productImgDao.saveBatch(prodImgEntityList);
            }
            List<ProductSkuDto> productSku = pdd.getProductSkuDto();
            if (productSku != null && productSku.size() > 0) {
                for (ProductSkuDto prodSku : productSku) {
                    String prodSkuId = UUIDUtil.get32UUID();
                    if (StringUtil.isBlank(prodSkuId)) {
                        throw new Exception("获取商品skuID异常！");
                    }
                    if (StringUtils.isEmpty(prodSku.getSkuId())) {
                        prodSku.setSkuId(prodSkuId);
                        prodSku.setProdId(pdd.getProdId());
                        productSkuDao.insertSelectiveDto(prodSku);
                    }
                    List<ProductSkuImgDTO> skuImg = prodSku.getProductSkuImgEntity();
                    List<ProductSkuImgEntity> skuImgEntity = new ArrayList<>();
                    if (skuImg != null && skuImg.size() > 0) {
                        for (ProductSkuImgDTO skuImgs : skuImg) {
                            if (StringUtil.isBlank(skuImgs.getSkuId())) {
                                String ImgProdSkuId = StringUtil.isBlank(prodSku.getSkuId()) ? prodSkuId : prodSku.getSkuId();
                                skuImgs.setSkuId(ImgProdSkuId);
                            }
                            skuImgs.setImgUrl(AliOSSUtil.getFilePath(skuImgs.getImgUrl()));

                            ProductSkuImgEntity temp = new ProductSkuImgEntity();
                            MyBeanUtils.copyProperties(skuImgs,temp);
                            skuImgEntity.add(temp);
                        }
                        productSkuImgDao.deleteByProdId(prodSku.getSkuId());
                        productSkuImgDao.saveBatch(skuImgEntity);
                    }
                }
            }
            //删除老的物流信息
            logisticsDao.deleteByProdId(pdd.getProdId());
            //添加商品物流信息
            addLogistics(pdd,pdd.getProdId());
        } else {
            pdd.setResponseCode(ResponseCodeEnum.PROD_STATUS_ERR);
        }
        return pdd;
    }

    @Override
    public List<Map<String, Object>> getStoreCats(ProductDto params) {
        //查询当前店铺经营范围的品类ID
        List<Map<String, Object>> catlist = productDao.getStoreCats(params);
        if (catlist == null || catlist.size() == 0) {
            throw new MsgException(ResponseCodeEnum.INVALID,"店铺经营范围不能为空");
        }
        if (catlist != null && catlist.size() > 0) {
            //查询品类表所有的数据
            List<ProdCategoryDTO> allcats = prodCategoryDao.getAllProdCategory();
            List<ProdCategoryDTO> allStoreCats = new ArrayList<>();
            //将属于当前店铺经营范围上下级的品类添加到一个新的list
            for (Map<String, Object> map : catlist) {
                String catId = map.get("prod_cat_id").toString();
                String catGrade = map.get("cat_grade").toString();
                for (int i = 0; i < allcats.size(); i++) {
                    String catIds = allcats.get(i).getProdCatId();
                    ProdCategoryDTO pc = allcats.get(i);
                    pc.setImgUrl(AliOSSUtil.getFileUrl(pc.getImgUrl(), AliOSSUtil.getNoEffictDateByDay(1)));
                    if ("3".equals(catGrade)) {
                        String oneCatId = catId.substring(0, 2);
                        String twoCatId = catId.substring(0, 5);
                        if (catId.equals(catIds) || twoCatId.equals(catIds) || oneCatId.equals(catIds)) {
                            allStoreCats.add(pc);
                        }
                    } else if ("2".equals(catGrade)) {
                        String oneCatId = catId.substring(0, 2);
                        if (oneCatId.equals(catIds) || (catIds.indexOf(catId) != -1)) {
                            allStoreCats.add(pc);
                        }
                    }
                }
            }
            //过滤重复数据
            HashSet sortSet = new HashSet(allStoreCats);
            allStoreCats.clear();
            allStoreCats.addAll(sortSet);
            //将属于当前店铺的品类递归成树
            return toTree(allStoreCats, 1, null);
        }
        return null;
    }

    @Override
    public PageInfo<ProductDto> getProductList(ProductDto pdd) throws Exception {
        PageHelper.startPage(pdd.getCurrentPage(), pdd.getPageSize());
        /*List<String> storeIdList = new ArrayList<>();
        if(!StringUtil.isBlank(pdd.getStoreName())){
            storeIdList = getStoreIdByName(pdd.getStoreName(),pdd.getMerchantId());
            logger.info(String.format("===>商户名称为%s的商户id为%s",pdd.getStoreName(),storeIdList));
        }*/
        List<ProductDto> result = productDao.queryPageList(pdd);
        //转换blob,商品状态,获取店铺列表ids
        if (result != null && result.size() > 0) {
            StringBuilder storeIds = new StringBuilder();
            storeIds.append("(");
            for (ProductDto dto : result) {
//                if (dto.getSpecParameters() != null) {
//                    dto.setSpecParameter(new String(dto.getSpecParameters(), "utf-8"));
//                    dto.setSpecParameters(null);
//                }
                dto.setProdStatusStr(StateUtil.changeProductStatus(dto.getProdStatus()));
                storeIds.append("\'").append(dto.getStoreId()).append("\'").append(",");
            }

            if (storeIds.length() > 1) {
                if (storeIds.length() > 0) {
                    /*catIds.delete(0,1);
                    catIds.delete(catIds.length() -2 ,catIds.length());*/
                    storeIds.delete(storeIds.length() - 1, storeIds.length());
                }
            }
            storeIds.append(")");
            String sql = "SELECT store_id,store_name FROM ec_store WHERE store_id IN " + storeIds;
            //转换商品品类id为平类名称
            //List<ProdCategoryDTO> catList = prodCategoryDao.findByCatIds(sql);
            List<StoreDTO> storeList = storeDao.findByCatIds(sql);
            if (storeList != null && storeList.size() > 0) {
                for (ProductDto productDto : result) {
                    String imgThumbnail = AliOSSUtil.getImgThumbnail(productDto.getAdImg(),null,null);
                    productDto.setAdImg(imgThumbnail);
                    //productDto.setAdImg(AliOSSUtil.getFileUrl(productDto.getAdImg(),AliOSSUtil.getNoEffictDateByDay(1)));
                    for (StoreDTO storeDTO : storeList) {
                        if(!StringUtil.isBlank(storeDTO.getStoreLogoUrl())) {
                            storeDTO.setStoreLogoUrl(AliOSSUtil.getFileUrl(storeDTO.getStoreLogoUrl(), AliOSSUtil.getNoEffictDateByDay(1)));
                        }
                        if (productDto.getStoreId().equals(storeDTO.getStoreId())) {
                            productDto.setStoreName(storeDTO.getStoreName());
                            //productDto.setProdCatName(categoryDto.getProdCatName());
                            break;
                        }
                    }
                }
            }

        }
        //获取品类列表ids
        PageInfo<ProductDto> page = new PageInfo<>(result);
        return page;
    }

    @Override
    public void deleteProduct(ProductStatusDTO dto) throws Exception {
        Integer num = productStatusDao.getProdStatus(dto.getProdId());
        if (num == 0) {
            throw new MsgException(ResponseCodeEnum.PROD_STATUS_ERR);
        }
        ProductStatusEntity entity = new ProductStatusEntity();
        entity.setProdId(dto.getProdId());
        entity.setOperatorId(dto.getOperatorId());
        entity.setProdStatus(4);
        entity.setProdUpdateTime(new Date());
        entity.setReason(dto.getReason());
        if (productStatusDao.updateByPrimaryKey(entity) > 0) {
            return;
        } else {
            throw new MsgException(ResponseCodeEnum.PROD_DELETE_ERR);
        }
    }

    @Override
    public void offShelfProduct(ProductStatusDTO dto) throws Exception {
        /*ProductStatusEntity entity = new ProductStatusEntity();
        entity.setProdId(dto.getProdId());
        entity.setOperatorId(dto.getOperatorId());
        entity.setProdStatus(2);
        entity.setProdUpdateTime(new Date());
        entity.setProdOutOfStockTime(new Date());
        entity.setReason(dto.getReason());*/
        if (productStatusDao.offShelfProduct(dto.getReason(), dto.getOperatorId(), dto.getProdId()) > 0) {//updateByPrimaryKey
            return;
        } else {
            throw new MsgException(ResponseCodeEnum.PROD_OFF_SHELF_ERR);
        }
    }

    @Override
    public void upShelfProduct(ProductStatusDTO dto) throws Exception {
        /*ProductStatusEntity entity = new ProductStatusEntity();
        entity.setProdId(dto.getProdId());
        entity.setOperatorId(dto.getOperatorId());
        entity.setProdStatus(1);
        entity.setProdUpdateTime(new Date());
        entity.setProdOutOfStockTime(new Date());
        entity.setReason(dto.getReason());*/
        if (productStatusDao.upShelfProduct(dto.getReason(), dto.getOperatorId(), dto.getProdId()) > 0) {//updateByPrimaryKey
            return;
        } else {
            throw new MsgException(ResponseCodeEnum.PROD_UP_SHELF_ERR);
        }
    }

    public List<Map<String, Object>> toTree(List<ProdCategoryDTO> prodCatDto, Integer servicePid, String parentId) {
        List<Map<String, Object>> results = new ArrayList<>();
        List<ProdCategoryDTO> temp = new ArrayList<>();
        if (servicePid == 1) {
            for (ProdCategoryDTO list : prodCatDto) {
                if (list.getCatGrade() != null && servicePid == list.getCatGrade()) {
                    temp.add(list);
                }
            }
        } else {
            for (ProdCategoryDTO list : prodCatDto) {
                if (list.getCatGrade() != null
                        && servicePid == list.getCatGrade()
                        && parentId.equals(list.getParentCatId())) {
                    temp.add(list);
                }
            }
        }
        if (temp.size() > 0) {
            for (ProdCategoryDTO cateDto : temp) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("id", cateDto.getProdCatId());
                map.put("name", cateDto.getProdCatName());
                List<Map<String, Object>> children
                        = toTree(prodCatDto, cateDto.getCatGrade() + 1, cateDto.getProdCatId());
                if (children.size() > 0) {
                    map.put("children", children);
                }
                results.add(map);
            }
        }
        return results;
    }

    @Override
    public Map<String, Object> releaseProduct(String prodCatId) {
        List<ProdBrandDTO> brandList = prodBrandDao.getCatBrands(prodCatId);
        if (brandList == null || brandList.size() == 0) {
            throw new MsgException(ResponseCodeEnum.INVALID, "不支持该品类商品发布，品牌为空");
        }

        List<ProdCatModelDTO> modelList = prodCatModelDao.getCatModelByCatId(prodCatId);
        if (modelList == null || modelList.size() == 0) {
            throw new MsgException(ResponseCodeEnum.INVALID, "不支持该品类商品发布，模板为空");
        }
        Map<String, Object> resMap = new HashMap<>();
        for (ProdBrandDTO temp : brandList) {
            temp.setProdBrandLogo(AliOSSUtil.getFileUrl(temp.getProdBrandLogo(),AliOSSUtil.getNoEffictDateByDay(1)));
        }
        resMap.put("brandList", brandList);

        //保存模板属性值待用
        List<ProdCatModelDTO> list = new ArrayList<>();
        list.addAll(modelList);
        //获取该分类下的模板列表
        for (int i = 0; i < modelList.size() - 1; i++) {
            for (int j = modelList.size() - 1; j > i; j--) {
                if (modelList.get(j).getModelId().equals(modelList.get(i).getModelId())) {
                    modelList.remove(j);
                }
            }
        }
        //遍历模板塞入模板详细值
        List<Map<String, Object>> map = new ArrayList<>();
        for (ProdCatModelDTO models : modelList) {
            Map<String, Object> maps = new HashMap<>();
            maps.put("prodPropName", models.getProdPropName());
            maps.put("seq", models.getSeq());
            maps.put("modelId", models.getModelId());
            List<Map<String, Object>> childrenMap = new ArrayList<>();
            for (ProdCatModelDTO allModel : list) {
                if (models.getModelId().equals(allModel.getModelId())) {
                    Map<String, Object> childrenMaps = new HashMap<>();
                    childrenMaps.put("cmId", allModel.getCmId());
                    childrenMaps.put("modelValue", allModel.getModelValue());
                    childrenMaps.put("isImg", allModel.getIsImg());
                    childrenMap.add(childrenMaps);
                }
            }
            maps.put("children", childrenMap);
            map.add(maps);
        }
        resMap.put("modelList", map);

        //添加物流列表
        List<LogisticsCompanyDTO> company = logisticsCompanyDao.logisticsCompanyList();
        if(company == null){
            company = new ArrayList<>();
        }
        resMap.put("logisticsCompanyListTemp",new ArrayList<>());//前端渲染数据需要
        resMap.put("logisticsCompanyList", company);
        return resMap;
    }

    private Map<String,BigDecimal> getMinAndMaxPrice(ProductDto dto){
        Map<String,BigDecimal> result = new HashMap<>();
        if(dto.getProductSkuDto() != null && dto.getProductSkuDto().size() > 0){
            Collections.sort(dto.getProductSkuDto(), new Comparator<ProductSkuDto>() {
                @Override
                public int compare(ProductSkuDto sku1, ProductSkuDto sku2) {
                    return sku1.getSkuPrice().compareTo(sku2.getSkuPrice());
                }
            });
        }
        List<ProductSkuDto> skuList = dto.getProductSkuDto();
        if(skuList != null && skuList.size() > 0){
            result.put("min",skuList.get(0).getSkuPrice());
            result.put("max",skuList.get(skuList.size()-1).getSkuPrice());
        }
        return result;
    }

    private void addLogistics(ProductDto pdd,String prodId){
        List<LogisticsDTO> logisticsList = pdd.getCheckedLogistics();
        List<LogisticsEntity> logistics = new ArrayList<>();
        if(logisticsList != null && logisticsList.size() > 0){
            for(LogisticsDTO logisticsDTO : logisticsList){
                LogisticsEntity entity = new LogisticsEntity();
                entity.setProdId(prodId);
                entity.setLcId(logisticsDTO.getLcId());
                entity.setIsDefault(logisticsDTO.getIsDefault());
                entity.setIsDelete(0);
                logistics.add(entity);
            }
        }
        logisticsDao.insertList(logistics);
    }

}
